/*
 * Copyright 2009-2011 Prime Technology.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.primefaces.examples.mobile;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.UploadedFile;
import org.primefaces.model.map.LatLng;

import au.com.cgu.RescueU.Claim;
import au.com.cgu.RescueU.ClaimController;
import au.com.cgu.RescueU.Location;
import au.com.cgu.RescueU.Photo;
import au.com.cgu.RescueU.PolicyController;
import au.com.cgu.RescueU.Registration;
import au.com.cgu.RescueU.RegistrationController;
import au.com.cgu.RescueU.ThirdParty;
import au.com.cgu.RescueU.Vehicle;

public class ClaimView implements Serializable{
    
	private Claim claim;
	private Registration registration = null;
	private String vehicle;
	private UploadedFile file;
	
	public ClaimView() {
		claim = new Claim();
		claim.setIncidentTime(new Date());
		claim.setThirdParty(new ThirdParty());
		claim.setLossLocation(new Location());
	}
	
	public Vehicle[] getVehicles()
	{
		return new PolicyController().findPolicyVehicles(getRegistration().getPolicyNumber());
	}
	
	public Registration getRegistration()
	{
		//new RegistrationController().
		
		if (registration == null)
		{
			
			Map<String, String> params =FacesContext.getCurrentInstance().
	                getExternalContext().getRequestParameterMap();
			String id = params.get("id");
			
			registration = new RegistrationController().getRegistrationDetails(id);
			claim.setPolicyNumber(registration.getPolicyNumber());
			claim.setPolicyHolder(registration.getPolicyHolder());
		}
		
		return registration;
	}

    public Claim getClaim() {
    	getRegistration();
        return claim;
    }

    public void setClaim(Claim claim) {
        this.claim = claim;
    }
    
    public String startClaim()
    {
    	return "lodgeClaim.jsf?id=" + registration.getID() + "&faces-redirect=true&transition=slideup";
    }
    
    public String lodgeClaim(){

    	ClaimController cc = new ClaimController();
    	claim = cc.lodge(claim);
    	return "lodged.jsf?claimNo=" + claim.getClaimNumber() + "&faces-redirect=true&transition=slideup";
    }

	public String getVehicle() {
		return vehicle;
	}

	public void setVehicle(String vehicle) {
		this.vehicle = vehicle;
	}
	
    public UploadedFile getFile() {
    	return file;
    }
	
    public void setFile(UploadedFile file) {
    	this.file = file;
        try {
        	if (file != null)
        	{
				InputStream inputstream = file.getInputstream();
				byte[] data = new byte[inputstream.available()];
				inputstream.read(data);
				claim.addPhoto(new Photo(file.getFileName(), null, data));
        	}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	public String getClaimNoFromURL()
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
		return params.get("claimNo");
	}
}